﻿using System;
using System.Collections.Generic;
using System.Data;
using Microsoft.Data.Sqlite;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using KorbeRTR.Utility;

namespace KorbeRTR.Models
{
    /// <summary>
    /// 用户及用户权限
    /// </summary>
    [Table(Name = "Users")]
    public class User
    {
        /// <summary>
        /// 用户ID
        /// </summary>
        [Column]
        public Int64 Id { get; set; }

        /// <summary>
        /// 用户名
        /// </summary>
        [Column]
        public string UserName { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        [Column]
        public string Password { get; set; }

        /// <summary>
        /// 软删除标志
        /// </summary>
        [Column]
        public bool IsDeleted { get; set; }

        /// <summary>
        /// 产品数据操作权限(新增/修改/删除)
        /// </summary>
        [Column(Name = "CPSJCZ")]
        public bool 产品数据操作权限 { get; set; }

        /// <summary>
        /// 生产料号操作权限(投料/变更/删除)
        /// </summary>
        [Column(Name = "SCLHCZ")]
        public bool 生产料号操作权限 { get; set; }

        /// <summary>
        /// 设备主控开关操作权限
        /// </summary>
        [Column(Name = "SBZKKGCZ")]
        public bool 设备主控开关操作权限 { get; set; }

        #region 参数设置
        /// <summary>
        /// 设备参数设定权限
        /// </summary>
        [Column(Name = "SBCSSD")]
        public bool 设备参数设定权限 { get; set; }

        /// <summary>
        /// 洗配槽参数设定权限
        /// </summary>
        [Column(Name = "XPCCSSD")]
        public bool 洗配槽参数设定权限 { get; set; }

        /// <summary>
        /// 温控器温度设定权限
        /// </summary>
        [Column(Name = "WKQWDSD")]
        public bool 温控器温度设定权限 { get; set; }

        /// <summary>
        /// 自动添加设定权限
        /// </summary>
        [Column(Name = "ZDTJSD")]
        public bool 自动添加设定权限 { get; set; }

        /// <summary>
        /// 累计值清零权限
        /// </summary>
        [Column(Name = "LJZQL")]
        public bool 累计值清零权限 { get; set; }

        /// <summary>
        /// 泵浦自动开关设定权限
        /// </summary>
        [Column(Name = "BPZDKGSD")]
        public bool 泵浦自动开关设定权限 { get; set; }

        /// <summary>
        /// 浮板位置设定权限
        /// </summary>
        [Column(Name = "FBWZSD")]
        public bool 浮板位置设定权限 { get; set; }
        #endregion

        /// <summary>
        /// 用户权限设定权限
        /// </summary>
        [Column(Name = "YHQXSD")]
        public bool 用户权限设定权限 { get; set; }

        /// <summary>
        /// 更改密码权限
        /// </summary>
        [Column(Name = "GGMM")]
        public bool 更改密码权限 { get; set; }

        /// <summary>
        /// 系统数据权限
        /// </summary>
        [Column(Name = "XTSJ")]
        public bool 系统数据权限 { get; set; }

        /// <summary>
        /// 退出软件权限
        /// </summary>
        [Column(Name = "RJTC")]
        public bool 软件退出权限 { get; set; }

        /// <summary>
        /// 故障代码设定权限
        /// </summary>
        [Column(Name = "GZDMSD")]
        public bool 故障代码设定权限 { get; set; }

        public User()
        {
            IsDeleted = false;

            产品数据操作权限 = false;
            生产料号操作权限 = false;
            设备主控开关操作权限 = false;

            设备参数设定权限 = false;
            洗配槽参数设定权限 = false;
            温控器温度设定权限 = false;
            自动添加设定权限 = false;
            累计值清零权限 = false;
            泵浦自动开关设定权限 = false;
            浮板位置设定权限 = false;

            用户权限设定权限 = false;
            更改密码权限 = false;

            系统数据权限 = false;

            软件退出权限 = false;

            故障代码设定权限 = false;
        }

        public static User Verify(string name, string password)
        {
            var dt = UserDBHelper.ExecuteTable("SELECT * FROM Users u WHERE u.UserName=@UserName AND u.IsDeleted=FALSE",
                new SqliteParameter("@UserName", name));

            if(dt.Rows.Count != 1)
            {
                return null;
            }

            return dt.Rows[0].ToModel<User>();
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <returns></returns>
        public static bool Add(User u)
        {
            int rows = UserDBHelper.ExecuteNonQuery("INSERT INTO Users(UserName,PassWord,CPSJCZ,SCLHCZ,SBZKKGCZ,SBCSSD,XPCCSSD,WKQWDSD,ZDTJSD,LJZQL,BPZDKGSD,FBWZSD,YHQXSD,GGMM,XTSJ,RJTC,GZDMSD)" +
                "VALUES(@UserName,@PassWord,@CPSJCZ,@SCLHCZ,@SBZKKGCZ,@SBCSSD,@XPCCSSD,@WKQWDSD,@ZDTJSD,@LJZQL,@BPZDKGSD,@FBWZSD,@YHQXSD,@GGMM,@XTSJ,@RJTC,@GZDMSD)",
                new SqliteParameter("@UserName", u.UserName),
                new SqliteParameter("@PassWord", u.Password),

                new SqliteParameter("@CPSJCZ", u.产品数据操作权限),
                new SqliteParameter("@SCLHCZ", u.生产料号操作权限),
                new SqliteParameter("@SBZKKGCZ", u.设备主控开关操作权限),

                new SqliteParameter("@SBCSSD", u.设备参数设定权限),
                new SqliteParameter("@XPCCSSD", u.洗配槽参数设定权限),
                new SqliteParameter("@WKQWDSD", u.温控器温度设定权限),
                new SqliteParameter("@ZDTJSD", u.自动添加设定权限),
                new SqliteParameter("@LJZQL", u.累计值清零权限),
                new SqliteParameter("@BPZDKGSD", u.泵浦自动开关设定权限),
                new SqliteParameter("@FBWZSD", u.浮板位置设定权限),

                new SqliteParameter("@YHQXSD", u.用户权限设定权限),
                new SqliteParameter("@GGMM", u.更改密码权限),

                new SqliteParameter("@XTSJ", u.系统数据权限),
                new SqliteParameter("@RJTC", u.软件退出权限),

                new SqliteParameter("@GZDMSD", u.故障代码设定权限)
                );
            return rows > 0;
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        public static bool Delete(User u)
        {
            int rows = UserDBHelper.ExecuteNonQuery("UPDATE Users SET IsDeleted=TRUE WHERE Id=@Id",
                new SqliteParameter("@Id", u.Id));
            return rows > 0;
        }

        public static bool Remove(User u)
        {
            int rows = UserDBHelper.ExecuteNonQuery("DELETE Users WHERE Id=@id",
                new SqliteParameter("@Id", u.Id));
            return rows > 0;
        }

        /// <summary>
        /// 更新
        /// </summary>
        public static bool Update(User u)
        {
            int rows = UserDBHelper.ExecuteNonQuery("UPDATE Users SET UserName=@UserName,PassWord=@PassWord,CPSJCZ=@CPSJCZ,SCLHCZ=@SCLHCZ,SBZKKGCZ=@SBZKKGCZ,SBCSSD=@SBCSSD,XPCCSSD=@XPCCSSD,WKQWDSD=@WKQWDSD,ZDTJSD=@ZDTJSD,LJZQL=@LJZQL,BPZDKGSD=@BPZDKGSD,FBWZSD=@FBWZSD,YHQXSD=@YHQXSD,GGMM=@GGMM,XTSJ=@XTSJ,RJTC=@RJTC,GZDMSD=@GZDMSD WHERE Id=@Id",
                new SqliteParameter("@Id", u.Id),
                new SqliteParameter("@UserName", u.UserName),
                new SqliteParameter("@PassWord", u.Password),

                new SqliteParameter("@CPSJCZ", u.产品数据操作权限),
                new SqliteParameter("@SCLHCZ", u.生产料号操作权限),
                new SqliteParameter("@SBZKKGCZ", u.设备主控开关操作权限),

                new SqliteParameter("@SBCSSD", u.设备参数设定权限),
                new SqliteParameter("@XPCCSSD", u.洗配槽参数设定权限),
                new SqliteParameter("@WKQWDSD", u.温控器温度设定权限),
                new SqliteParameter("@ZDTJSD", u.自动添加设定权限),
                new SqliteParameter("@LJZQL", u.累计值清零权限),
                new SqliteParameter("@BPZDKGSD", u.泵浦自动开关设定权限),
                new SqliteParameter("@FBWZSD", u.浮板位置设定权限),

                new SqliteParameter("@YHQXSD", u.用户权限设定权限),
                new SqliteParameter("@GGMM", u.更改密码权限),

                new SqliteParameter("@XTSJ", u.系统数据权限),
                new SqliteParameter("@RJTC", u.软件退出权限),

                new SqliteParameter("@GZDMSD", u.故障代码设定权限)
                );
            return rows > 0;
        }

        /// <summary>
        /// 列出全部用户
        /// </summary>
        /// <returns></returns>
        public static List<User> ListAll()
        {
            var dt = UserDBHelper.ExecuteTable("SELECT * FROM Users WHERE IsDeleted=FALSE");
            Console.WriteLine(dt);

            return dt.ToModel<User>();
        }
    }

    public static class UserExtersion
    {
        public static bool Check产品基本数据操作权限(this User user)
        {
            return (user!=null)? user.产品数据操作权限 : false;
        }

        public static bool Check生产料号操作权限(this User user)
        {
            return (user != null) ? user.生产料号操作权限 : false;
        }

        public static bool Check设备主控开关操作权限(this User user)
        {
            return (user != null) ? user.设备主控开关操作权限 : false;
        }

        #region 参数设定
        public static bool Check设备参数设定权限(this User user)
        {
            return (user != null) ? user.设备参数设定权限 : false;
        }

        public static bool Check洗配槽参数设定权限(this User user)
        {
            return (user != null) ? user.洗配槽参数设定权限 : false;
        }

        public static bool Check温控器温度设定权限(this User user)
        {
            return (user != null) ? user.温控器温度设定权限 : false;
        }

        public static bool Check自动添加设定权限(this User user)
        {
            return (user != null) ? user.自动添加设定权限 : false;
        }

        public static bool Check累计值清零权限(this User user)
        {
            return (user != null) ? user.累计值清零权限 : false;
        }

        public static bool Check泵浦自动开关设定权限(this User user)
        {
            return (user != null) ? user.泵浦自动开关设定权限 : false;
        }

        public static bool Check浮板位置设定权限(this User user)
        {
            return (user != null) ? user.浮板位置设定权限 : false;
        }
        #endregion

        public static bool Check用户权限设定权限(this User user)
        {
            return (user != null) ? user.用户权限设定权限 : false;
        }

        public static bool Check更改密码权限(this User user)
        {
            return (user != null) ? user.更改密码权限 : false;
        }


        public static bool Check系统数据权限(this User user)
        {
            return (user != null) ? user.系统数据权限 : false;
        }


        public static bool Check软件退出权限(this User user)
        {
            return (user != null) ? user.软件退出权限 : false;
        }


        public static bool Check故障代码设定权限(this User user)
        {
            return (user != null) ? user.故障代码设定权限 : false;
        }
    }
}
